<template>
  <div class="dialog">
    <el-dialog
      :title="'仓库'+titleText"
      :visible.sync="showDialog"
      width="35%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item
          label="疾病:"
          prop="diseaseBankId"
          class="w100p"
        >
          <el-select
            v-model="formData.diseaseBankId"
            placeholder="请选择疾病"
            filterable
            clearable
          >
            <el-option
              v-for="item in DiseaseData"
              :key="item.diseaseBankId"
              :label="item.diseaseName"
              :value="item.diseaseBankId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="文件:"
          prop="fileId"
          class="w100p"
        >
          <el-select
            v-model="formData.fileId"
            placeholder="请选择文件"
            filterable
            clearable
          >
            <el-option
              v-for="item in FileData"
              :key="item.fileId"
              :label="item.fileName"
              :value="item.fileId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述：" class="w100p" prop="description">
          <el-input
            v-model="formData.description"
            clearable
            :rows="2"
            type="textarea"
            placeholder="请输入描述"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import DiseaseBankApi from "@/api/basic-info-manage/diseaseBank"
import demoApi from "@/api/document";
import DiseaseBankFileApi from "@/api/basic-info-manage/diseaseBankFile"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      FileData:[],
      DiseaseData:[],
      formData: {},
      rules: {
        storeHouseName: [
          { required: true, message: "请输入仓库名称", trigger: ["change", "blur"] }
        ],
        area: [
          { required: true, message: "请输入仓库面积", trigger: ["change", "blur"] }, {
            validator: checkInt, trigger: ["change", "blur"] }
        ],
        // location: [
        //   { required: true, message: "请输入仓库位置", trigger: ["change", "blur"] }
        // ]
      },
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryDisease()
    this.queryFile()
  },
  methods: {
    queryFile(){
      let params = {
        page:1,
        size:1000,
      }
      demoApi.queryPageByCondition(params).then(res=>{
        console.log('---文件查询---',res);
        this.FileData = res.data.content;
      })
    },
    queryDisease(){
      let params = {
        page:1,
        size:1000,
      }
      DiseaseBankApi.queryPageByCondition(params).then(res=>{
        console.log('---疾病查询---',res);
        this.DiseaseData = res.data.content;
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            DiseaseBankFileApi.edit(params).then(res => {
              console.log('---编辑品种---',res);
            });
          } else if (this.nowChanceStau === "add") {
            DiseaseBankFileApi.add(this.formData).then(res => {
              console.log('---添加品种---',res);
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
